//开发
const path = require("path")
const fs = require("fs")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const srcRoot = path.resolve(__dirname, 'src')
const devPath = path.resolve(__dirname, 'dev')
const pageDir = path.resolve(srcRoot, 'page')
const mainFile = 'index.js'

function gethtmlArray(entryMap){
    let htmlArray = [];
    Object.keys(entryMap).forEach((key)=>{
        let fullPathName = path.resolve(pageDir, key)
        let fileName = path.resolve(fullPathName, key + ".html")
        
        if (fs.existsSync(fileName)){
            htmlArray.push(new HtmlWebpackPlugin({
                filename: key + '.html',
                template: fileName,
                chunks: [key]
            }))
        }
    })
    return htmlArray
}


function getEntry(){
    let entryMap = {}
    
    fs.readdirSync(pageDir).forEach((pathname)=>{
        // fullPathName = ~/src/page/index
        let fullPathName = path.resolve(pageDir, pathname)
        let stat = fs.statSync(fullPathName)
        // fileName = ~/src/page/index/index.js
        let fileName = path.resolve(fullPathName, mainFile)
        if (stat.isDirectory() && fs.existsSync(fileName)){
            // entryMap = {index: '~/src/page/index/index.js'}
            entryMap[pathname] = fileName
        }
    })
    return entryMap
}

const entryMap = getEntry()
const htmlArray = gethtmlArray(entryMap)
module.exports = {
    mode: "development",
    devServer: {
        contentBase: devPath
    },
    entry: entryMap,
    resolve:{
        alias: {
            component: path.resolve(srcRoot, 'components')
        },
        extensions: ['.js', '.jsx']
    },
    output: {
        path: devPath,
        filename: '[name].min.js'
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
                include: srcRoot
            },
            {
                test: /\.scss$/,
                use: ['style-loader', 'css-loader', 'sass-loader', {
                    loader: 'sass-resources-loader',
                    options: {
                        resources: srcRoot + '/components/common.scss'
                    }
                }],
                include: srcRoot
            },
            {
                test: /\.(png|jpg|jpeg)$/,
                use: 'url-loader?limit=8192',
                include: srcRoot
            },
            {
                test: /\.(js|jsx)$/,
                use: [
                    {loader: 'babel-loader'},
                    {loader: 'eslint-loader'}
                ],
                include: srcRoot
            }
        ]
    },
    plugins: [
        
    ].concat(htmlArray)
}


